package clientset

import (
	cachev1alpha1 "gitverse.ru/synapse/kubelatte/pkg/api/v1alpha1"
	"k8s.io/client-go/kubernetes/scheme"
	"k8s.io/client-go/rest"
)

//go:generate go run github.com/vektra/mockery/v2@v2.20.2 --name=V1Alpha1Interface
type V1Alpha1Interface interface {
	Template(namespace string) TemplateInterface
	Trigger(namespace string) TriggerInterface
	Scope(namespace string) ScopeInterface
	TriggerInstance() TriggerInstanceInterface
}

type V1Alpha1Client struct {
	RestClient            rest.Interface
	TemplateClient        TemplateInterface
	TriggerClient         TriggerInterface
	ScopeClient           ScopeInterface
	TriggerInstanceClient TriggerInstanceInterface
}

func NewForConfig(c *rest.Config) (*V1Alpha1Client, error) {

	config := *c
	config.ContentConfig.GroupVersion = &cachev1alpha1.GroupVersion
	config.APIPath = "/apis"
	config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
	config.UserAgent = rest.DefaultKubernetesUserAgent()

	client, err := rest.RESTClientFor(&config)
	if err != nil {
		return nil, err
	}

	return &V1Alpha1Client{RestClient: client}, nil
}

func (c *V1Alpha1Client) Template(namespace string) TemplateInterface {
	if c.TemplateClient == nil {
		c.TemplateClient = &templateClient{
			restClient: c.RestClient,
			ns:         namespace,
		}
	}
	return c.TemplateClient
}

func (c *V1Alpha1Client) Trigger(namespace string) TriggerInterface {
	if c.TriggerClient == nil {
		c.TriggerClient = &triggerClient{
			restClient: c.RestClient,
			ns:         namespace,
		}
	}
	return c.TriggerClient
}

func (c *V1Alpha1Client) Scope(namespace string) ScopeInterface {
	if c.ScopeClient == nil {
		c.ScopeClient = &scopeClient{
			restClient: c.RestClient,
			ns:         namespace,
		}
	}
	return c.ScopeClient
}

func (c *V1Alpha1Client) TriggerInstance() TriggerInstanceInterface {
	if c.TriggerInstanceClient == nil {
		c.TriggerInstanceClient = &triggerInstanceClient{
			restClient: c.RestClient,
		}
	}
	return c.TriggerInstanceClient
}
